import store from './index.js'

export default {
  namespaced: 'play-video',
  state: () => ({
    // 当前播放的歌曲（-1为未播放）
    musicId: -1,
  }),
  mutations: {
    // 设置当前播放的音乐id
    setMusicId(state, musicId) {
      state.musicId = musicId
    }
  },
  actions: {
    // 获取下一首要播放歌曲的id
    getNextMusicId(store) {
      let musicId = store.state.musicId
      // musicId不存在，返回空
      if (!(musicId && musicId !== -1)) return null
      
      let activeList = store.rootGetters['music-list/activeList']
      let music = activeList.music || []
      let index = music.findIndex(i => i.id == musicId)
      
      // 如果未在歌单中找到歌曲，或已是最后一首 返回空
      if (index === -1 || index === music.length - 1) return null
      return music[index + 1].id
    }
  }
}